package cn.bugstack.springframework.beans.factory.support;

import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.PropertyValues;
import cn.bugstack.springframework.beans.factory.config.AutowireCapableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanPostProcessor;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.hutool.core.bean.BeanUtil;

import java.lang.reflect.Constructor;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: TODO
 * @date 2022/8/7 12:11
 */
public abstract class AbstractAutowireCableBeanFactory extends AbstractBeanFactory implements AutowireCapableBeanFactory {


    InstantiationStrategy instantiationStrategy  = new CglibSubclassingInstantiationStrategy();





    @Override
    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
        Object result = existingBean;
        for(BeanPostProcessor postProcessor : getBeanPostProcessors()){
            Object current = postProcessor.PostProcessorsBeforeInitialization(result, beanName);
            if(current == null){
                return  result;
            }
            result = current;
        }
        return null;
    }

    @Override
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
        Object result = existingBean;
        for (BeanPostProcessor postProcessor : getBeanPostProcessors()){
            Object current = postProcessor.postProcessorsAfterInitialization(result, beanName);
            if(current == null){
                return result;
            }
            result = current;
        }
        return null;
    }




    @Override
    Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
        Object bean = null;

        try {
            bean = createBeanInstance(beanName, beanDefinition, args);
            // 给Bean填充属性
            applyPropertyValues(beanName,bean,beanDefinition);
            // 执行Bean的初始化方法和BeanPostProcessor 的前置和后置处理方法
            bean = initializeBean(beanName,bean,beanDefinition);
        } catch (NoSuchMethodException e) {
            throw new BeansException("初始化bean失败...");
        }
        addSingleton(beanName,bean);
        return bean;

    }

    private Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition) {
        return null;
    }

    private void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) throws BeansException {
        PropertyValues propertyValues = beanDefinition.getPropertyValues();
        for (PropertyValue value : propertyValues.getPropertyValues()){
            String propertyName = value.getPropertyName();
            // 相当于 ref
            Object propertyValue = value.getPropertyValue();
            // 如果A类依赖B类,则
            if(propertyValue instanceof BeanReference){
                BeanReference beanReference = (BeanReference)propertyValue;
                propertyValue = getBean(beanReference.getBeanName());
            }
            // 属性填充
            BeanUtil.setFieldValue(bean, propertyName, propertyValue);
        }
    }

    private Object createBeanInstance(String beanName, BeanDefinition beanDefinition, Object[] args) throws NoSuchMethodException, BeansException {
        Constructor constructor = null;
        Class beanClass = beanDefinition.getBeanClass();
        Constructor [] declaredConstructor = beanClass.getDeclaredConstructors();
        // 给实例化对象的构造函数赋值
        for(Constructor c : declaredConstructor){
            if(args != null && c.getParameterTypes().length == args.length){
                constructor = c;
                break;
            }
        }
        return  instantiationStrategy.instantiate(beanDefinition,beanName,constructor);
    }
}
